package app.fugema.shop.main.login.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.base.common.model.http.err.ApiException;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.span.SpanUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.LoginPasswordFragmentBinding;

import app.fugema.shop.main.login.model.MUserInfo;
import app.fugema.shop.main.login.vm.LoginVM;
import app.fugema.shop.util.SimpleTextWatcher;

public class LoginPasswordFragment extends BaseFragment<LoginPasswordFragmentBinding, LoginVM> {


    @Override
    protected LoginPasswordFragmentBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.login_password_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        binding.tvPhoneLogin.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {

                //UIUtils.showToastSafes("手机号登录");

                RegisterActivity.lunch(mActivity);
                //mActivity.finish();

                /*
                if (getActivity() instanceof LoginActivity)
                {
                    LoginActivity loginActivity = (LoginActivity) getActivity();
                    loginActivity.setCurrentItem(0);
                }
                */
            }
        });

        binding.rtvLogin.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                login();
            }
        });

        binding.tvPasswordForget.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                ForgetPassWordActivity.lunch(getContext(), "忘记密码");
            }
        });

        binding.etPhoneNO.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                //stateChange(s.toString());
            }
        });
        // 设置手机号 格式
        //VerifyUtils.onTextChangedPhone(binding.etPhoneNO);

        String s = binding.etPhoneNO.getText().toString().trim();
        if (s.length() == 0)
        {
            binding.rtvLogin.setEnabled(false);
            binding.rtvLogin.setAlpha(0.6f);
        }

        binding.etPassWord.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(String content)
            {
                String phone = binding.etPhoneNO.getText().toString().trim();
                stateChange(phone,content);
            }
        });


        /*   服务协议 放到注册页面
        binding.llCheckedText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.ctvCheckedText.setChecked(!binding.ctvCheckedText.isChecked());
                stateChange(binding.etPhoneNO.getText().toString().trim());
                if (binding.ctvCheckedText.isChecked())
                {
                    binding.ctvCheckedText.setBackgroundResource(R.mipmap.ic_radio_checked);
                }
                else
                {
                    binding.ctvCheckedText.setBackgroundResource(R.mipmap.ic_radio_unchecked);
                }
            }
        });
        */
    }



    private void stateChange(String phone ,String pwd)
    {
        //VerifyUtils.checkPhoneNumber(phone) binding.ctvCheckedText.isChecked()
        if ( UIUtils.isNotEmpty( phone ) && UIUtils.isNotEmpty( pwd ) )
        {
            binding.rtvLogin.setEnabled(true);
            binding.rtvLogin.setAlpha(1f);
        } else {
            binding.rtvLogin.setEnabled(false);
            binding.rtvLogin.setAlpha(0.6f);
        }
    }

    /**
     *  账户密码登录
     */
    public void login()
    {
        String phone = binding.etPhoneNO.getText().toString().trim();
        String password = binding.etPassWord.getText().toString();
        String err = null;
                //viewModel.checkedErrStr(viewModel.checkedPhone(phone), viewModel.checkedPassword(password));
        if (err == null)
        {
            viewModel.login(phone, password)
                     .observe(this, new BaseViewObserver<LiveDataWrapper<MUserInfo>>(getActivity()) {
                        @Override
                        protected void onSuccess(LiveDataWrapper<MUserInfo> data)
                        {
                            if( data.data.isSuccess() )
                            {
                                if( data.data.getCode() == 103 )
                                {
                                    UIUtils.showToastSafesClose(data.data.getMsg());
                                }
                                else
                                {
                                    UIUtils.showToastSafes("登录成功");
                                    mActivity.finish();
                                }
                            }
                        }

                         @Override
                         protected void onAppError(ApiException error)
                         {
                             UIUtils.showToastSafes(error.getMessage());
                         }
                     });
        }
        else
        {
            UIUtils.showToastSafes(err);
        }

    }
}
